package com.wangp.mywrite.s_netty.nio.ch1;

import java.nio.ByteBuffer;

import static com.wangp.mywrite.s_netty.nio.ch1.ByteBufferUtil.debugAll;

/**
 * @description: TODO
 * @author tooStronger
 * @date 2023/3/15
 */
public class ByteBufferRead {

    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        byteBuffer.put(new byte[]{
                'a','b','c','d'
        });
        byteBuffer.flip();


        // 从头开始读
        byteBuffer.get(new byte[4]);
        debugAll(byteBuffer);
        byteBuffer.rewind();
        System.out.println((char) byteBuffer.get());


        // mark & reset
        // mark 记录 position 的位置； reset 重置到 mark 的位置
/*        System.out.println((char) byteBuffer.get());
        System.out.println((char) byteBuffer.get());
        byteBuffer.mark();// 加标记到索引为2的位置
        System.out.println((char) byteBuffer.get());
        System.out.println((char) byteBuffer.get());
        byteBuffer.reset();// 重置到索引2
        System.out.println((char) byteBuffer.get());
        System.out.println((char) byteBuffer.get());*/


        // get(i) 不会变更索引位置
 /*       System.out.println((char) byteBuffer.get(3));
        debugAll(byteBuffer);*/
    }
}
